package com.pkk.penguin.frame.know.springmvc.controller;

import com.pkk.penguin.frame.know.springmvc.entity.UserRegister;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by peikunkun on 2018/1/7 0007.
 */
@RequestMapping(value = "fileController")
@Controller
public class FileController {


  @RequestMapping(value = "userRegister")
  public String userRegister(HttpServletRequest request, @ModelAttribute UserRegister userRegister, Model model) {
    if (!userRegister.getImage().isEmpty()) {
      System.out.println("文件正确");
      String path = request.getContextPath();
      System.out.println("路径为:" + path);
      /*文件原始名称*/
      String fileName = userRegister.getImage().getOriginalFilename();
      File newFile = new File(path, fileName);
      if (!newFile.getParentFile().exists()) {
        newFile.getParentFile().mkdirs();
      }
      try {
        path = "F:" + File.separator + "测试文件夹";
        /*输出到一个新的文件夹中，具体api见下面*/
        userRegister.getImage().transferTo(new File(path + File.separator + fileName));
      } catch (IOException e) {
        e.printStackTrace();
      }
      model.addAttribute("userRegister", userRegister);
    } else {
      model.addAttribute("result", "储存失败");
    }
    return "/fileupload/uploadresult";
  }


  @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
  public String uploadFile(HttpServletRequest request,
      @RequestParam(value = "filedesc", required = false) String fileDesc,
      @RequestParam(value = "uploadFile") MultipartFile file, Model model) {

    if (!file.isEmpty()) {
      System.out.println("文件正确");
      String path = request.getContextPath();
      System.out.println("路径为:" + path);
      /*文件原始名称*/
      String fileName = file.getOriginalFilename();
      File newFile = new File(path, fileName);
      if (!newFile.getParentFile().exists()) {
        newFile.getParentFile().mkdirs();
      }
      try {
        path = "F:" + File.separator + "测试文件夹";
        /*输出到一个新的文件夹中，具体api见下面*/
        file.transferTo(new File(path + File.separator + fileName));
      } catch (IOException e) {
        e.printStackTrace();
      }
      System.out.println("保存成功");
      model.addAttribute("result", "储存成功");
    } else {
      model.addAttribute("result", "储存失败");
      System.out.println("文件错误");
    }
    return "/fileupload/uploadresult";
  }

  @RequestMapping(value = "download")
  public ResponseEntity<byte[]> downloadFile(HttpServletRequest request,
      @RequestParam(value = "filename", required = true) String filename, Model model) throws IOException {
    String path = path = "F:" + File.separator + "测试文件夹";
    File file = new File(path + File.separator + filename);
    HttpHeaders httpHeaders = new HttpHeaders();
    /*下载时的文件，解决中文乱码*/
    String downFileName = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
    /*通知浏览器以attachment方式打开图片*/
    httpHeaders.setContentDispositionFormData("attachment", downFileName);
    /*二进制流数据（下载最长见的方式）*/
    httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    /*设置http的协议状态和设置相应的Context-Type*/
    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.CREATED);
  }


}
